package cfca.paperless.client.servlet._9_unite;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;

import cfca.paperless.client.bean.ImageBean;
import cfca.paperless.client.bean.SealUserInfo;
import cfca.paperless.client.bean.SignLocation;
import cfca.paperless.client.servlet.PaperlessConfig;
import cfca.paperless.client.servlet.PaperlessUniteClient;
import cfca.paperless.client.util.IoUtil;
import cfca.paperless.client.util.PaperlessUtil;
import cfca.paperless.client.util.StrategyUtil;

/**
 * @Description 联合接口(htmlToPdf,合成业务数据,自动化签章)
 * @author clsu
 * @CodeReviewer TODO
 */
public class PaperlessUniteTest01 {

    public static void main(String[] args) throws Exception {
        try {
            long timeStart = System.currentTimeMillis();// 开始时间
            
            PaperlessUniteClient clientBean = new PaperlessUniteClient(PaperlessConfig.uniteUrl, 10000, 60000);

            //////////////////////////////////html转pdf///////////////////////////////////
            String fileType = "html";
            // 待转换成PDF文件的HTML文件，
            String htmlPath = "./TestData/file.html";
            byte[] htmlData = FileUtils.readFileToByteArray(new File(htmlPath));
            clientBean.addTransformToPdfParams(htmlData, fileType);
            
            ////////////////////////////合成图片(synthesizeAutoBusinessPdf)///////////////////////////////
            String imageBeanListXml = PaperlessUtil.buildImageBeanListXml(getImageBeanList());
            clientBean.addSynthesizeAutoBusinessPdfParams("", "", imageBeanListXml);
            
            /////////////////////////////////蓋章(sealAutoPdf)////////////////////////////////////////
            String sealCode = "org001";//0613sm201
            String sealPassword = "11111111";
            // 可以为空，不为空则此图片会替代此处印章的印模图片
            SealUserInfo sealUserInfo = new SealUserInfo("张三", "beijing", "reason");
            // 3=关键字签章
            SignLocation signLocation = new SignLocation(1, 100, 100);//new SignLocation("人签名", "R", 0, 0);
            // 生成签章策略文件
            String sealStrategyXml = StrategyUtil.createSealStrategyXml(sealCode, sealPassword, "", sealUserInfo, signLocation);
            clientBean.addSealAutoPdfParams(sealStrategyXml);
            
            
            // 操作员编码或者机构号
            String operatorCode = PaperlessConfig.operatorCode;
            // 渠道编码，可以为空
            String channelCode = PaperlessConfig.channelCode;
            
            byte[] sealedPdfData = clientBean.uniteReqAndRes(operatorCode, channelCode);

            String errorRsString = PaperlessUtil.isError(sealedPdfData);

            // 处理结果为正常，保存签章后的PDF文件到本地目录下
            if ("".equals(errorRsString)) {
                System.out.println("OK:sealAutoPdf");
                System.out.println(sealedPdfData.length);
                IoUtil.write("./TestData/output/sealedPdf-uniteReqAndRes.pdf", sealedPdfData);
            } else {
                // 处理结果为异常，打印出错误码和错误信息
                System.out.println("NG:sealAutoPdf");
                System.out.println(sealedPdfData.length);
                System.out.println(new String(sealedPdfData, "UTF-8"));
            }
            
            long timeEnd = System.currentTimeMillis();// 结束时间
            System.out.println("time:" + (timeEnd - timeStart));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private static List<ImageBean> getImageBeanList() throws Exception {
        List<ImageBean> imageBeanList = new ArrayList<ImageBean>();
        // 如下代码是在pdf中添加可以显示的图片
        byte[] image = FileUtils.readFileToByteArray(new File("./TestData/seal.png"));
        byte[] identificationFrontData = FileUtils.readFileToByteArray(new File("./TestData/1.png"));
        byte[] identificationBackData = FileUtils.readFileToByteArray(new File("./TestData/2.png"));
        
        ImageBean imageBean0 = new ImageBean("100", "100", "签章", "C", 0, 0, image);
        ImageBean imageBean = new ImageBean("100", "100", 1, "400", "300", identificationFrontData);
        ImageBean imageBean2 = new ImageBean("100", "100", 1, "100", "300", identificationBackData);
         imageBeanList.add(imageBean0);
        imageBeanList.add(imageBean);
        imageBeanList.add(imageBean2);
        return imageBeanList;
    }

}
